<template>
  <a-modal
      v-model:visible="visible"
      :title="title"
      :mask-closable="false"
      :esc-to-close="false"
      :modal-style="{ maxWidth: '520px' }"
      width="90%"
      @before-ok="save"
      @close="reset"
  >
    <a-form :model="form" :auto-label-width="true" layout="horizontal">
      <a-space direction="vertical" :style="{ width: '300px' }" style="margin: 0 auto">
        <a-form-item field="type" :style="{ width: '300px' }" label="状态">
          <a-select v-model="form.status" placeholder="请选择状态">
            <a-option :value="0">已送达</a-option>
            <a-option :value="1">已查阅</a-option>
            <a-option :value="2">不匹配</a-option>
            <a-option :value="3">一面</a-option>
            <a-option :value="4">二面</a-option>
            <a-option :value="5">三面</a-option>
            <a-option :value="6">HR面</a-option>
            <a-option :value="7">已录用</a-option>
          </a-select>
        </a-form-item>
      </a-space>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import {Message} from '@arco-design/web-vue'
import {getJobPositionResume, addJobPositionResume, updateJobPositionResume} from '@/apis'
import {type Columns, GiForm, type Options} from '@/components/GiForm'
import {useForm} from '@/hooks'
import {useDict} from '@/hooks/app'

const emit = defineEmits<{
  (e: 'save-success'): void
}>()

const dataId = ref('')
const isUpdate = computed(() => !!dataId.value)
const title = computed(() => (isUpdate.value ? '修改投递状态' : '新增投递状态'))
const formRef = ref<InstanceType<typeof GiForm>>()


const options: Options = {
  form: {},
  col: {xs: 24, sm: 24, md: 24, lg: 24, xl: 24, xxl: 24},
  btns: {hide: true}
}

const columns: Columns = [
  {
    label: '投递状态',
    field: 'status',
    type: 'input',
  },
]

const {form, resetForm} = useForm({
  status: '',
})

// 重置
const reset = () => {
  formRef.value?.formRef?.resetFields()
  resetForm()
}

const visible = ref(false)
// 新增
const onAdd = () => {
  reset()
  dataId.value = ''
  visible.value = true
}

// 修改
const onUpdate = async (id: string) => {
  reset()
  dataId.value = id
  const res = await getJobPositionResume(id)
  Object.assign(form, res.data)
  visible.value = true
}

// 保存
const save = async () => {
  try {
    const isInvalid = await formRef.value?.formRef?.validate()
    if (isInvalid) return false
    if (isUpdate.value) {
      await updateJobPositionResume(form, dataId.value)
      Message.success('修改成功')
    } else {
      await addJobPositionResume(form)
      Message.success('新增成功')
    }
    emit('save-success')
    return true
  } catch (error) {
    return false
  }
}

defineExpose({onAdd, onUpdate})
</script>
